﻿using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using TestEnvirementCL.POCO;
using TestEnvirementCL.POCO.Users;
using System.Data.Entity.Infrastructure;
namespace TestEnvirementCL.Concrete
{
  public class EFDbContext : DbContext
  {
    //DataTables
    public DbSet<Advertisement> Advertisements { get; set; }
    public DbSet<Country> Countries { get; set; }
    public DbSet<Event> Events { get; set; }
    public DbSet<Meeting> Meetings { get; set; }
    public DbSet<NSSF> NSSFs { get; set; }
    public DbSet<Record> Records { get; set; }
    public DbSet<Region> Regions { get; set; }
    public DbSet<Result> Results { get; set; }
    public DbSet<School> Schools { get; set; }
    public DbSet<Sport> Sports { get; set; }
    public DbSet<Team> Teams { get; set; }

    //Users DataTables
    public DbSet<Coach> Coaches { get; set; }
    public DbSet<NSSFRep> NSSFReps { get; set; }
    public DbSet<ISFAdmin> ISFAdmins { get; set; }
    public DbSet<SCC> SCCs { get; set; }
    public DbSet<Student> Students { get; set; }
    
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

      modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
      // 1-1 Relations definitions
      modelBuilder.Entity<School>().HasOptional(s => s.SCC);
      modelBuilder.Entity<SCC>().HasRequired(s => s.School);
      modelBuilder.Entity<NSSF>().HasOptional(n => n.NSSFRep);
      modelBuilder.Entity<NSSFRep>().HasRequired(n => n.NSSF);
      modelBuilder.Entity<Region>().HasOptional(r => r.NSSF);
      modelBuilder.Entity<NSSF>().HasRequired(n => n.Region);
      
      //ForeignKey problem solving
     /* modelBuilder.Entity<Meeting>().HasOptional(m => m.Coach)
                                    .WithOptionalPrincipal();
      modelBuilder.Entity<Meeting>().HasOptional(m => m.NSSF)
                                    .WithOptionalPrincipal();
      /*modelBuilder.Entity<Record>().HasRequired(r => r.Meeting)
                                    .WithMany()
                                    .HasForeignKey(m => m.MeetingId).WillCascadeOnDelete(false);
      */
      // Cascade settings
     modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
      
    }

  }
}
